<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   彩票页面
 */
defined('IN_YuLin') || exit('NO PERMIT!');

if($a == 'view')
{
	if(!$_uid)
	{
		header('Location: '.MU('login'));
		exit;
	}
	
    $id=intval($_GET['id']);
    $data=$db->getrow('SELECT * FROM '.Table('lottery').' WHERE status = 1 AND id = '.$id);
	!$data && ShowMsg('该彩种不存在！',SU(''));

	$tplnames = [
		//1	=>	'shishicai',	//	三分时时彩
		2	=>	'cqssc',	//	重庆时时彩
		3	=>	'bjpk10',	//	北京PK10
		4	=>	'xy28',	//	幸运28
		//5	=>	'bj28',	//	北京28
		//6	=>	'3fenpk10',	//	三分PK10
		8	=>	'gd11x5',	//	广东11选5
		9	=>	'xjssc',	//	新疆时时彩
		10	=>	'sd11x5',	//	山东11选5
		11	=>	'tjssc',	//	天津时时彩
		12	=>	'sh11x5',	//	上海11选5
		13	=>	'jx11x5',	//	江西11选5
		14	=>	'ahk3',	//	安徽快3
		15	=>	'fc3d',	//	福彩3D
		16	=>	'shssl',	//	上海时时乐
		17	=>	'fcpl3',	//	福彩排列三
	];
	
	!array_key_exists($data['id'],$tplnames) && ShowMsg('查看的彩票不存在！',SU());
	
	//	当前期号
    $period = $db->getrow('SELECT * FROM '.Table('lottery_period').' WHERE cid = '.$id.' AND stoptime > '.TIMESTAMP.' ORDER BY stoptime');
	$period['leftsec'] = max(0,$period['stoptime'] - 30 - TIMESTAMP);	//	提前30秒结束投注
	
	//	上一期结果
	$lastperiod = $db->getrow('SELECT * FROM '.Table('lottery_period').' WHERE cid = '.$id.' AND stoptime < '.TIMESTAMP.' ORDER BY stoptime DESC');
	
	//	玩法
	$plays = $db->getall('SELECT * FROM '.Table('lottery_play').' WHERE cid IN (0,'.$id.') AND status = 1 ORDER BY id');
	
	$tpl->display('wap/index_view_'.$tplnames[$data['id']]);
}
else if($a == 'more')	//	所有彩票
{
    $lottery = $db->getall('SELECT * FROM '.Table('lottery').' WHERE status = 1 ORDER BY id');
	foreach($lottery as $key => $val)
	{
		//	最新期号
		$val['period'] = $db->getrow('SELECT * FROM '.Table('lottery_period').' WHERE cid = '.$val['id'].' AND stoptime > '.TIMESTAMP.' ORDER BY stoptime');
		$val['leftsec'] = max(0,$val['period']['stoptime'] - TIMESTAMP);
		
		//	上一期
		$val['last'] = $db->getrow('SELECT * FROM '.Table('lottery_period').' WHERE cid = '.$val['id'].' AND stoptime < '.TIMESTAMP.' ORDER BY stoptime DESC');
		
		$lottery[$key] = $val;
	}

    $tpl->display('wap/index_more');
}
else if($a == 'draw')	//	开奖大厅
{
	$lottery = $db->getall('SELECT * FROM '.Table('lottery').' WHERE status = 1 ORDER BY id');
	foreach($lottery as $key => $val)
	{
		//	已开奖
		$val['last'] = $db->getrow('SELECT * FROM '.Table('lottery_period').' WHERE cid = '.$val['id'].' AND rednum <> \'\' ORDER BY stoptime DESC');
		$val['last']['rednum'] = explode(',',$val['last']['rednum']);
		
		$lottery[$key] = $val;
	}
	
    $tpl->display('wap/draw');
}
else if($a == 'drawlog')	//	开奖记录
{
	$id = intval($_GET['id']);
	!$id && ShowMsg('彩票不存在！',MU());
	
	$lottery = $db->getrow('SELECT * FROM '.Table('lottery').' WHERE id = '.$id.' AND status = 1');
	!$lottery && ShowMsg('彩票不存在！',MU());
	
	$list = $db->getall('SELECT * FROM '.Table('lottery_period').' WHERE cid = '.$id.' ORDER BY id DESC LIMIT 20');
	
    $tpl->display('wap/drawlog');
}
else
{
	$lottery = $db->getall('SELECT * FROM '.Table('lottery').' WHERE recommend = 1 AND status = 1 ORDER BY id');
	foreach($lottery as $key => $val)
	{
		//	最新期号
		$val['period'] = $db->getrow('SELECT * FROM '.Table('lottery_period').' WHERE cid = '.$val['id'].' AND stoptime > '.TIMESTAMP.' ORDER BY stoptime');
		$val['leftsec'] = max(0,$val['period']['stoptime'] - TIMESTAMP);
		
		$lottery[$key] = $val;
	}
	
	//	最新公告
	$notice = $db->getrow('SELECT * FROM '.Table('article').' WHERE cid = 3 AND status = 1 ORDER BY id DESC');
	
	//	最新中奖
	$prize = $db->getall('SELECT t.prize,u.username,c.title FROM '.Table('lottery_order').' t LEFT JOIN '.Table('user').' u ON t.uid = u.id LEFT JOIN '.Table('lottery').' c ON t.cid = c.id WHERE t.prize > 0 AND t.status = 1 ORDER BY t.id DESC LIMIT 20');
	
    $tpl->display('wap/index');
}